﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DOTNET_ASSIGNMENT.Models;

namespace DOTNET_ASSIGNMENT.Services
{
    public class CustomerServices
    {
        public IEnumerable<Customer> GetCustomers()
        {
            using (DataContext db = new DataContext())
            {
               return db.Customers.ToList();
            }
        }

        public List<string> GetCustomerInfo(string email)
        {
            using (DataContext db = new DataContext())
            {
                var item = db.Customers.Single(x => x.Email == email);
                if (item != null)
                {
                    return new List<string>
                    {
                        item.Email,
                        item.Password,
                    };
                }
                else
                    return new List<string> { };
            }
        }

        // Add customer mới
        public bool AddCustomer(Customer cus)
        {     
            using (DataContext db = new DataContext())
            {
                try
                {
                    cus.Date = DateTime.Now.ToShortDateString();
                    cus.IsBlocked = false;
                    db.Customers.Add(cus);
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool DeleteCustomer(string email)
        {
            bool deleteSuccess = false;
            using (DataContext db = new DataContext())
            {
                try
                {
                    var item = db.Customers.Single(x => x.Email == email);
                    if (item != null)
                    {
                        db.Set<Customer>().Remove(item);
                        db.SaveChanges();
                        deleteSuccess = true;
                    }
                }
                catch
                {
                    return false;
                }
                return deleteSuccess;
            }
        }
 
        /// <summary>
        /// So sánh xem email có tồn tại reong hệ thống chưa
        /// </summary>
        /// <param name="email"></param>
        /// <returns>Nếu trùng trả về true, không tar3 về false</returns>
        /// 
        public bool CompareEmail(string email)
        {

            using (DataContext db = new DataContext())
            {
                var item = db.Customers.Single(x => x.Email == email);
                return (item != null) ? true : false;
            }

        }
        
        public Customer GetCustomer(string email)
        {
            using (DataContext db = new DataContext())
            {
                var item = db.Customers.Single(x => x.Email == email);
                if (item != null)
                {
                    return item;
                }
                return null;
            }

        }

        public bool ChangePassword(string email, string oldPassword, string newPassword, string confirmPassword)
        {
            using (DataContext db = new DataContext())
            {

                var item = db.Customers.Single(x => x.Email == email);
                if (item != null)
                {
                    if (item.Password == oldPassword)
                    {
                        if (newPassword == confirmPassword)
                        {
                            item.Password = newPassword;
                            item.ConfirmPassword = confirmPassword;
                            db.SaveChanges();
                            return true;
                        }
                    }
                }
                return false;
            }
        }

        public Customer IsValid(string email, string password)
        {
            using (DataContext db = new DataContext())
            {
                var item = db.Customers.Single(x => x.Email == email);
                if (item != null)
                {
                    if (item.Password == password)
                    {
                        return item;
                    }
                }
            }
            return null;
        }

        public bool Block(string email)
        {
            using (DataContext db = new DataContext())
            {
                var cus = db.Customers.Single(x => x.ConfirmEmail == email);

                if (cus == null) return false;

                cus.IsBlocked = true;
                db.SaveChanges();
                return true;
            }
        }

        public bool UnBlock(string email)
        {
            using (DataContext db = new DataContext())
            {
                var cus = db.Customers.Single(x => x.ConfirmEmail == email);

                if (cus == null) return false;

                cus.IsBlocked = false;
                db.SaveChanges();
                return true;
            }
        }

        public bool Edit(Customer customer)
        {
            using (DataContext db = new DataContext())
            {
                var cus = db.Customers.Single(x => x.ConfirmEmail == customer.Email);

                if (cus == null) return false;

                cus.Email = customer.Email;
                cus.ConfirmPassword = customer.ConfirmPassword;
                cus.Date = customer.Date;
                cus.ID = cus.ID;
                cus.Password = customer.Password;
                cus.ConfirmPassword = customer.ConfirmPassword;
                cus.IsBlocked = customer.IsBlocked;
                db.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// Đổi thông tin User
        /// </summary>
        /// <param name="email"></param>
        /// <returns>Nếu thành công trả về true, không trả về false</returns>
        //public bool ChangeInfo(string email, string name, string phone)
        //{
        //    using (DataContext db = new DataContext())
        //    {

        //        var item = db.Customers.Single(x => x.Email == email);
        //        if (item != null)
        //        {
        //            if (!String.IsNullOrWhiteSpace(name))
        //            {
        //                item.CustomerName = name/*.Trim()*/;
        //            }

        //            if (!String.IsNullOrWhiteSpace(phone/*.Trim()*/))
        //            {
        //                item.CustomerPhone = phone/*.Trim()*/;
        //            }
        //            db.SaveChanges();
        //            return true;
        //        }
        //        return false;
        //    }
        //}


        //public bool SetNewPassword(string email, string newPassword)
        //{
        //    ICryptoService cryptoService = new PBKDF2();
        //    const int SALT_SIZE = 16;
        //    const int HASH_ITERATIONS = 50;
        //    using (CustomerContext _db = new CustomerContext())
        //    {
        //        var customer = _db.Customers.Find(email);
        //        if (customer != null)
        //        {
        //            //a new password hash is generated from a generated salt with the default settings
        //            customer.CustomerPassword = cryptoService.Compute(newPassword, SALT_SIZE, HASH_ITERATIONS);
        //            //assigning the generated salt to the user
        //            customer.CustomerPasswordSalt = cryptoService.Salt;
        //            _db.SaveChanges();
        //            return true;
        //        }
        //    }
        //    return false;
        //}

        //public bool ValidatePassword(Customer user, string password)
        //{
        //    ICryptoService cryptoService = new PBKDF2();
        //    //hash the password with the saved salt for that user
        //    string hashed = cryptoService.Compute(password.ToString(), user.CustomerPasswordSalt.ToString());
        //    //return true if both hashes are the same
        //    return (hashed == user.CustomerPassword) ? true : false;
        //}
    }
}